/**
 * 
 */
package com.util;

import java.util.Date;

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;

/**
 * @author jinmei.wang
 *
 */
public class MemCachedUtil {
	
	    // 创建全局的唯一实例
	    protected static MemCachedClient mcc = new MemCachedClient();
	     
	    protected static MemCachedUtil _instance = new MemCachedUtil();
	     
	    // 设置与缓存服务器的连接池
	    static {
	        // 服务器列表和其权重，个人memcached地址和端口号
	       // String[] servers = {"121.52.215.132:11211", "220.194.56.110:11211"};
	    	String[] servers = {"106.186.31.50:11211"};
	        Integer[] weights = {1};
	 
	        // 获取socke连接池的实例对象
	        SockIOPool pool = SockIOPool.getInstance();
	 
	        // 设置服务器信息
	        pool.setServers( servers );
	        pool.setWeights( weights );
	 
	        // 设置初始连接数、最小和最大连接数以及最大处理时间
	        pool.setInitConn( 5 );
	        pool.setMinConn( 5 );
	        pool.setMaxConn( 250 );
	        pool.setMaxIdle( 1000 * 60 * 60 * 6 );
	 
	        // 设置主线程的睡眠时间
	        pool.setMaintSleep( 30 );
	 
	        // 设置TCP的参数，连接超时等
	        pool.setNagle( false );
	        pool.setSocketTO( 3000 );
	        pool.setSocketConnectTO( 0 );
	 
	        // 初始化连接池
	        pool.initialize();
	    }
	     
	    /**
	     * 保护型构造方法，不允许实例化！
	     *
	     */
	    protected MemCachedUtil()
	    {
	         
	    }
	     
	    /**
	     * 获取唯一实例.
	     * @return
	     */
	    public static MemCachedUtil getInstance()
	    {
	        return _instance;
	    }
	     
	    /**
	     * 添加一个指定的值到缓存中.
	     * @param key
	     * @param value
	     * @return
	     */
	    public boolean add(String key, Object value)
	    {
	        return mcc.add(key, value);
	    }
	     
	    public boolean add(String key, Object value, Date expiry)
	    {
	        return mcc.add(key, value, expiry);
	    }

	    public boolean add(String key, Object value, int expiry)
	    {
	        return mcc.add(key, value, expiry);
	    }
	     
	    /**
	     * 替换一个指定的值到缓存中.
	     * @param key
	     * @param value
	     * @return
	     */
	    public boolean replace(String key, Object value)
	    {
	        return mcc.replace(key, value);
	    }
	     
	    public boolean replace(String key, Object value, Date expiry)
	    {
	        return mcc.replace(key, value, expiry);
	    }

	    public boolean replace(String key, Object value, int expiry)
	    {
	        return mcc.replace(key, value, expiry);
	    }
	    
	    /**
	     * 删除一个指定的值到缓存中.
	     * @param key
	     * @param value
	     * @return
	     */
	    public boolean delete(String key)
	    {
	        return mcc.delete(key);
	    }
	     
	     
	    /**
	     * 根据指定的关键字获取对象.
	     * @param key
	     * @return
	     */
	    public Object get(String key)
	    {
	        return mcc.get(key);
	    }
	     
	    /**
	     * 是否存在指定key
	     * @param key
	     * @return
	     */
	    public boolean keyExists(String key){
	    	return mcc.keyExists(key);
	    }
	    public static void main(String[] args)
	    {
	    	MemCachedUtil cache = MemCachedUtil.getInstance();
	        cache.add("zf", 18);
	        System.out.println("zf get value1 : " + cache.get("zf"));
	        cache.replace("zf", 19);
	        //cache.delete("zf");
	        System.out.println("zf get value2 : " + cache.get("zf"));
	    }
}
